<template>
  <div id="hotList">
    <!-- 上部搜索条件 -->
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <!-- 操作按钮部分 -->
    <div class="table-conBtn">
      <el-button
        v-permission="'wh:system:heatRecommend:add'"
        type="success"
        icon="el-icon-plus"
        @click="addBtn"
      >
        新增
      </el-button>
      <el-button
        v-permission="'wh:system:heatRecommend:import'"
        type="warning"
        icon="el-icon-upload2"
        @click="importBtn"
      >
        导入
      </el-button>
      <el-button
        v-permission="'wh:system:heatRecommend:remove'"
        type="warning"
        plain
        @click="deletetBtn"
      >
        批量删除
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
    </div>
    <!-- 表格部分 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      @selection-change="selectChange"
      max-height="490"
      ref="table"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column type="index" width="60" label="序号">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column prop="userId" label="userId"></el-table-column>
      <el-table-column prop="avatar" label="头像">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="用户名"></el-table-column>
      <el-table-column label="手机号">
        <template slot-scope="scope">
          <div v-if="scope.row.phone">
            {{ scope.row.phone }}
          </div>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="dapengAccount" label="大鹏号"></el-table-column>
      <el-table-column label="微信号">
        <template slot-scope="scope">
          <div v-if="scope.row.weiXinAccount">
            {{ scope.row.weiXinAccount }}
          </div>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column label="QQ号">
        <template slot-scope="scope">
          <div v-if="scope.row.qqAccount">
            {{ scope.row.qqAccount }}
          </div>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="userType" label="用户类型">
        <template slot-scope="scope">
          <span v-if="scope.row.userType">{{
            scope.row.userType | filterName('hotType')
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="homeCount"
        label="提交作业数量"
        width="110"
        align="center"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.homeCount">
            {{ scope.row.homeCount }}
          </div>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="workCount"
        label="发布动态数量"
        align="center"
        width="110"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.workCount">
            {{ scope.row.workCount }}
          </div>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="fansCount" align="center" label="粉丝数量">
        <template slot-scope="scope">
          <div v-if="scope.row.fansCount">
            {{ scope.row.fansCount }}
          </div>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建人">
        <template slot-scope="scope">
          <div v-if="scope.row.createdBy">
            {{ scope.row.createdBy }}
          </div>
          <div v-else>
            -
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createdDate" label="创建时间" min-width="150">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="90">
        <template slot-scope="scope">
          <el-button
            v-permission="'wh:system:heatRecommend:remove'"
            type="text"
            @click="deleteOne(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
    <!-- 新增弹窗 -->
    <addDialog
      v-if="addDialogData.isShow"
      :add-dialog-data="addDialogData"
      @refresh="refresh"
    ></addDialog>
    <!-- 导入弹窗 -->
    <importDialog
      v-if="importDialogData.isShow"
      :dialog-data="importDialogData"
      @refresh="refresh"
      @showError="showError"
    ></importDialog>
    <!-- 导入失败弹窗 -->
    <errorDialog
      v-if="errorDialogData.isShow"
      :dialog-data="errorDialogData"
      from="hotFailed"
    ></errorDialog>
  </div>
</template>
<script>
import {
  getSearchList,
  batchRemove,
  deleteReq,
  importUrl,
  downUrl
} from '@/api/hotManagement/hotManageAPI' // 接口
import topSearch from '@component/hotManagement/hotRecommend/topSearch' // 上部搜索条件
import addDialog from '@component/hotManagement/hotRecommend/addDialog' // 新增弹窗
import importDialog from '@common/importDialog' // 导入弹窗
import errorDialog from '@common/errorDialog' // 导入失败弹窗
import { saveScrollMixins } from '@/mixins/saveScroll'
export default {
  name: 'hotRecommend',
  msg: '关注热门推荐',
  mixins: [saveScrollMixins],
  components: {
    topSearch,
    addDialog,
    importDialog,
    errorDialog
  },
  data() {
    return {
      loading: true,
      total: 0,
      tableData: [], // table内容
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      // 搜索条件
      formInline: {
        userType: '', // 用户属性,默认全部
        type: '', // 查询类型,默认手机号
        value: '' // 查询
      },
      // 新增
      addDialogData: {
        isShow: false, // 默认隐藏
        type: '', // 默认新增
        params: '' // 编辑时，传递的参数
      },
      // 导入失败弹框的数据
      errorDialogData: {
        isShow: false, // 控制弹框显示
        warningText: '' // 参数
      },
      // 导入弹窗
      importDialogData: {
        title: '创建关注热门推荐',
        isShow: false, // 默认隐藏
        params: '' // 编辑时，传递的参数
      },
      selectData: [] // 行
    }
  },
  activated() {
    this.toTableScrollHeight()
  },
  mounted() {
    // 获取列表
    this.searchList()
    this.$nextTick(() => {
      setTimeout(() => {
        this.setTableScrollHeight()
      }, 1000)
    })
  },
  methods: {
    /* 获取列表 */
    searchList() {
      this.loading = true
      let obj = Object.assign({}, this.searchParams, this.formInline)
      getSearchList(
        obj,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // 新增按钮点击事件
    addBtn() {
      this.addDialogData.isShow = true
    },
    /* 多选删除按钮 */
    deletetBtn() {
      if (this.selectData.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      this.$confirm(
        `确认要删除选中的${this.selectData.length}条数据吗?`,
        '删除提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let ids = []
        this.selectData.forEach((item) => {
          ids.push(item.id)
        })
        let obj = { ids }
        batchRemove(
          obj,
          (response) => {
            this.searchList()
          },
          (response) => {
            this.$message({
              message: response.message,
              type: 'warning'
            })
          }
        )
      })
    },
    /* 单选删除按钮 */
    deleteOne(id) {
      this.$confirm(`确定删除该条关注热门推荐信息吗？`, '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteReq(
          id,
          (response) => {
            console.log(response)
            this.searchList()
          },
          (response) => {
            this.$message({
              message: response.message,
              type: 'warning'
            })
          }
        )
      })
    },
    // 选择框改变事件
    selectChange(value) {
      this.selectData = value
      this.isDisabled = !(this.selectData.length > 0)
    },
    /* 刷新列表 */
    refresh() {
      this.toSearch()
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },
    // 导入按钮点击事件
    importBtn() {
      this.importDialogData.url = importUrl() // 导入接口
      this.importDialogData.downUrl = downUrl() // 下载模板接口
      this.importDialogData.isShow = true
    },
    /* 导入失败弹窗 */
    showError(text) {
      this.errorDialogData.isShow = true // 显示导入失败弹框
      this.errorDialogData.warningText = text // 显示导入失败弹框
    },
    // 分页当前页改变事件
    handleCurrentChange(page) {
      this.searchParams.pageNum = page
      this.searchList()
    }
  }
}
</script>
<style lang="less"></style>
